<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="联系电话" prop="mobilePhone">
        <el-input
          v-model="queryParams.mobilePhone"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择维修类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="维修进度" prop="status" v-hasPermi="['repairs:repairs:remove']">
        <el-select v-model="queryParams.status" placeholder="请选择维修进度" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['repairs:repairs:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['repairs:repairs:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['repairs:repairs:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['repairs:repairs:export']"
        >导出</el-button>
      </el-col>-->
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="联系电话" align="center" prop="mobilePhone" width="120" />
      <el-table-column label="维修地点" align="center" prop="place" />
      <el-table-column label="详细地址" align="center" prop="detailedAddress" />
      <el-table-column label="维修类型" align="center" prop="type" />
      <el-table-column label="问题描述" align="center" >
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.questionDetailed" placement="bottom-end">
               <div style="overflow:hidden;text-overflow: ellipsis;-webkit-line-clamp: 1;display: -webkit-box;-webkit-box-orient: vertical;">
                 {{scope.row.questionDetailed}}
               </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="故障图片集合" align="center" width="100">
        <template slot-scope="scope">
          <el-image
            style="width: 60px; height: 60px"
            :src="scope.row.urls[0]"
            :preview-src-list="scope.row.urls">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="维修进度" align="center" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            v-if="scope.row.status==0"
            style="padding: 4px 6px;"
            type="warning"
          >{{statusFormat(scope.row)}}</el-button>
          <el-button
            size="mini"
            v-else-if="scope.row.status==1"
            style="padding: 4px 6px;"
            type="info"
            :disabled="true"
          >{{statusFormat(scope.row)}}</el-button>
          <el-button
            size="mini"
            v-else
            style="padding: 4px 6px;"
            type="success"
            :disabled="true"
          >{{statusFormat(scope.row)}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="数据创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180">
        <template slot-scope="scope">
         <div>
           <el-button
             size="mini"
             v-if="scope.row.status==0"
             style="padding: 4px 6px;background-color: #e65959;"
             type="danger"
             @click="openSelectUser(scope.row)"
             v-hasPermi="['repairs:repairs:remove']"
           >分配员工</el-button>
           <div v-else-if="scope.row.status==1 || scope.row.status==2">
             
           </div>
           <el-button
             size="mini"
             style="padding: 4px 6px;background-color: #409eff;border-color: #409eff;"
             type="primary"
             icon="el-icon-view"
             v-if="scope.row.status==1 || scope.row.status==2"
             @click="lookUser(scope.row)"
             v-hasPermi="['repairs:repairs:remove']"
           >查看信息</el-button>
           <el-button
             size="mini"
             style="padding: 4px 6px;"
             type="danger"
             v-if="scope.row.status==1"
             :disabled="true"
             v-hasPermi="['repairs:repairs:remove']"
           >处理中</el-button>
         </div>
         <div style="margin-top: 5px;">
            <el-button
               size="small"
               type="primary"
               v-show="scope.row.status==1"
               style="padding: 4px 10px;background-color: #e43131;border-color: #e43131;"
               @click="finish(scope.row)"
               v-hasPermi="['repairs:repairs:edit']"
             >标记完成</el-button>
         </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="联系电话" prop="mobilePhone">
          <el-input v-model="form.mobilePhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="维修地点" prop="place">
          <el-input v-model="form.place" placeholder="请输入维修地点" />
        </el-form-item>
        <el-form-item label="详细地址" prop="detailedAddress">
          <el-input v-model="form.detailedAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="维修类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择维修类型" v-if="typeOptions.length>0">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述" prop="questionDetailed">
          <el-input v-model="form.questionDetailed" placeholder="请输入问题描述" />
        </el-form-item>
        <el-form-item label="故障图片集合" prop="urls">
          <el-input v-model="form.urls" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="维修进度" prop="status">
          <el-select v-model="form.status" placeholder="请选择维修进度">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open2" width="600px" append-to-body>
      <el-form :model="form2" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称">
              <el-input :disabled="true" v-model="form2.nickName" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码" >
              <el-input :disabled="true" v-model="form2.phonenumber" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱">
              <el-input :disabled="true" v-model="form2.email" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14">
            <el-form-item label="用户名称">
              <el-input :disabled="true" v-model="form2.userName" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户性别">
              <el-select :disabled="true" v-model="form2.sex" >
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group :disabled="true" v-model="form2.status">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="角色">
              <el-select :disabled="true" v-model="form2.roleIds" multiple>
                <el-option
                  v-for="item in roleOptions"
                  :key="item.roleId"
                  :label="item.roleName"
                  :value="item.roleId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input :disabled="true" v-model="form2.remark" type="textarea"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open2=false">关闭</el-button>
      </div>
    </el-dialog>


    <select-user ref="select" @ok="handleQuery" />

  </div>
</template>

<script>
import { listRepairs, getRepairs, delRepairs, addRepairs, updateRepairs, exportRepairs } from "@/api/repairs/repairs";
import { getUser } from "@/api/system/user";
import { getUserProfile } from "@/api/system/user";
import selectUser from "./selectUser";
export default {
  name: "Repairs",
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  components: {
    selectUser
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修信息表格数据
      repairsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 维修类型:0-宿舍报修；1-网络报修字典
      typeOptions: [],
      // 维修进度: 0-待处理,1-处理中,2-已完成,3-已撤销字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mobilePhone: null,
        type: null,
        urls: null,
        status: null,
      },
      // 表单参数
      form: {},
      form2: {},
      // 角色选项
      roleOptions: [],
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    /* getUserProfile().then(response => {
      console.log('访问身份->', response.roleGroup)
      if(response.roleGroup.indexOf("维修员工") == -1){
        this.maintainManager = true
      }
    }); */
    this.getList();
    this.getDicts("repair_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("repair_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 打开维修员工用户表弹窗 */
    openSelectUser(row) {
      this.$refs.select.show(row.id);
    },
    /** 查询维修信息列表 */
    getList() {
      this.loading = true;
      listRepairs(this.queryParams).then(response => {
        let list = response.rows
        for(let i=0; i<list.length; i++){
          list[i].urls = list[i].urls != null ? list[i].urls.split(',') : []
        }
        this.repairsList = list;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 维修进度: 0-待处理,1-处理中,2-已完成,3-已撤销字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 获取字典文本
    selectDictLabel(options, value){
      return options[value].dictLabel
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mobilePhone: null,
        place: null,
        detailedAddress: null,
        type: null,
        questionDetailed: null,
        urls: null,
        status: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRepairs(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维修信息";
      });
    },
    finish(row){
      this.$confirm('是否确认任务已处理完成呢?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          updateRepairs({id: row.id, status: 2}).then(response => {
            this.$message({
              message: '操作成功,已标记为处理完成',
              type: 'success'
            });
          });
        }).then(() => {
          this.getList();
        })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRepairs(this.form).then(response => {
              this.$message({
                message: '修改成功',
                type: 'success'
              });
              this.open = false;
              this.getList();
            });
          } else {
            addRepairs(this.form).then(response => {
              this.$message({
                message: '新增成功',
                type: 'success'
              });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除维修信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRepairs(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有维修信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRepairs(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    //查看用户
    lookUser(row){
      getUser(row.sysUserEmpId).then(response => {
        this.form2 = response.data;
        console.log('form2', response.data)
        this.roleOptions = response.roles;
        this.form2.roleIds = response.roleIds;
        this.open2 = true;
        this.title = "维修员工信息详情";
        this.form2.password = "";
      });
    },
  }
};
</script>
